﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for Window2.xaml which is the window the user will use to choose the answers/elements
    /// in the assignment.
    /// </summary>
    public partial class Window2 : Window
    {
        private Assignment myAssignment;

        public Window2(Assignment assignment)
        {
            InitializeComponent();
            AddAssignment(assignment);
            textBox1.Text = myAssignment.AssignmentText;
            comboBox1.ItemsSource = myAssignment.GetAnswerlist();
        }

        /// <summary>
        /// Method used for passing an assignment from the first step window to this one.
        /// </summary>
        public void AddAssignment(Assignment assignment)
        {
            myAssignment = assignment;
        }

        /// <summary>
        /// Creates the third step window if all requirements are met.
        /// </summary>
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (myAssignment.GetAnswerlist().Count > 0)
            {
                sortAnswers(myAssignment.GetAnswerlist());
                myAssignment.AssignmentText = textBox1.Text;
                var step3 = new Window3(myAssignment);
                this.Close();
                step3.Show();
            }
            else
                MessageBox.Show("Du skal minimum tilføje et tomt område.");
        }

        /// <summary>
        /// Adds a part of the text in the textbox to the answerlist depending on the assignmenttype
        /// </summary>
        private void button2_Click(object sender, RoutedEventArgs e)
        {
            switch (myAssignment.GetType())
            {
                case 0:
                    addWordToAnswerList();
                    sortAnswers(myAssignment.GetAnswerlist());
                    comboBox1.Items.Refresh();
                    break;
                case 1:
                    addWordForSpellingToAnswerList();
                    sortAnswers(myAssignment.GetAnswerlist());
                    comboBox1.Items.Refresh();
                    break;
            }
        }

        /// <summary>
        /// Returns the number of answers that has been added to the assignment text so far.
        /// </summary>
        private int findNumbersOfOccurences(string s)
        {
            int i = 0;
            int count = 0;
            while((i = s.IndexOf("________________", i)) != -1)
            {
                i += 16;
                count++;
            }
            return count;
        }

        /// <summary>
        /// Sorts the answers in the answerList depending on their occurence in the assignment text
        /// </summary>
        /// <param name="answers"></param>
        private void sortAnswers(List<Answer> answers)
        {
            answers.Sort(delegate(Answer a1, Answer a2) { return a1.AnswerNumber.CompareTo(a2.AnswerNumber); });
        }

        /// <summary>
        /// Method for adding a word to the answerlist if the assignmenttype is Quiz
        /// </summary>
        private void addWordToAnswerList()
        {
            if (textBox1.SelectedText.Length < 17 && textBox1.SelectedText.Length > 0 && textBox1.SelectedText.Contains("_") == false
                        && textBox1.SelectedText.First().ToString() != " " && textBox1.SelectedText.Last().ToString() != " ")
            {
                myAssignment.AddAnswer(textBox1.SelectedText);
                var i = textBox1.SelectionStart;
                myAssignment.GetAnswerlist().Last().AnswerNumber = findNumbersOfOccurences(textBox1.Text.Substring(0, i)) + 1;
                foreach (Answer answer in myAssignment.GetAnswerlist())
                {
                    if (answer.AnswerNumber >= myAssignment.GetAnswerlist().Last().AnswerNumber && answer != myAssignment.GetAnswerlist().Last())
                    {
                        answer.AnswerNumber += 1;
                    }
                }
                textBox1.Text = textBox1.Text.Remove(textBox1.SelectionStart, textBox1.SelectionLength);
                textBox1.Text = textBox1.Text.Insert(i, " ________________ ");
            }
            else
            {
                MessageBox.Show("Mulige fejl:" + Environment.NewLine + Environment.NewLine + 
                                "Et spørgsmål må max bestå af 16 symboler"
                               + Environment.NewLine + Environment.NewLine +
                               "Du skal have markeret et stykke tekst."
                               + Environment.NewLine + Environment.NewLine +
                               "Du må ikke vælge noget der allerede er del af et spørgsmål altså tegnet '_'"
                               + Environment.NewLine + Environment.NewLine +
                               "Det valgte ord må ikke starte eller ende på et mellemrum");
            }
        }

        /// <summary>
        /// Adds a word to the answerlist if the assignmenttype is spellingAssignment
        /// </summary>
        private void addWordForSpellingToAnswerList()
        {
            if (textBox1.SelectedText.Length < 17 && textBox1.SelectedText.Length > 0 && textBox1.SelectedText.Contains("_") == false
                        && textBox1.SelectedText.Contains(" ") == false)
            {
                myAssignment.AddAnswer(textBox1.SelectedText);
                var i = textBox1.SelectionStart;
                myAssignment.GetAnswerlist().Last().AnswerNumber = findNumbersOfOccurences(textBox1.Text.Substring(0, i)) + 1;
                foreach (Answer answer in myAssignment.GetAnswerlist())
                {
                    if (answer.AnswerNumber >= myAssignment.GetAnswerlist().Last().AnswerNumber && answer != myAssignment.GetAnswerlist().Last())
                    {
                        answer.AnswerNumber += 1;
                    }
                }
                textBox1.Text = textBox1.Text.Remove(textBox1.SelectionStart, textBox1.SelectionLength);
                textBox1.Text = textBox1.Text.Insert(i, " ________________ ");
            }
            else
            {
                MessageBox.Show("Mulige fejl:" + Environment.NewLine + Environment.NewLine +
                                "Et spørgsmål må max bestå af 16 symboler"
                               + Environment.NewLine + Environment.NewLine +
                               "Du skal have markeret et stykke tekst."
                               + Environment.NewLine + Environment.NewLine +
                               "Du må ikke vælge noget der allerede er del af et spørgsmål altså tegnet '_'"
                               + Environment.NewLine + Environment.NewLine +
                               "Det valgte ord må ikke indeholde et mellemrum");
            }
        }

        /// <summary>
        /// Deletes an answer from the answer list and puts it back to its original spot in the assignmenttext
        /// </summary>
        private void button3_Click(object sender, RoutedEventArgs e)
        {
            int index = 0;
            if (comboBox1.SelectedIndex >= 0)
            {
                for (int i = 0; i <= comboBox1.SelectedIndex; i++)
                {
                    index = findAnswerLocation(index) + 1;
                }
                textBox1.Text = textBox1.Text.Remove(index - 1, 18);
                textBox1.Text = textBox1.Text.Insert(index - 1, comboBox1.SelectedValue.ToString());
                myAssignment.GetAnswerlist().RemoveAt(comboBox1.SelectedIndex);
                comboBox1.Items.Refresh();
            }
        }

        /// <summary>
        /// Finds the first occurence of the symbol marking an answer in the assignmenttext given a starting point
        /// </summary>
        private int findAnswerLocation(int i)
        {
            return textBox1.Text.IndexOf(" ________________ ", i);
        }

        /// <summary>
        /// Help button telling how to use the current window
        /// </summary>
        private void button4_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("I dette vindue skal de dele af teksten der skal være skjult for eleven, vælges." +
                            System.Environment.NewLine + System.Environment.NewLine +
                            "Dette gøres ved at trække musen hen over en del af teksten og"
                            + " markere den for derefter at trykke på 'Marker Del Som Spørgsmål' knappen." +
                            System.Environment.NewLine + System.Environment.NewLine +
                            "Valgte dele vil blive udskiftet med en lang streg, og vil blive puttet i listen nederst i vinduet." +
                            System.Environment.NewLine +
                            System.Environment.NewLine +
                            "Vælges et ord i listen og trykkes der på 'Slet spørgsmål' knappen vil ordet fjernes fra listen og blive smidt på sin plads"
                            + " i teksten igen." + System.Environment.NewLine + System.Environment.NewLine +
                            "Når alle ønskede skjulte dele er fundet trykkes der på 'Næste Trin' knappen");
        }
    }
}
